package com.dxy.lc.controller;

import com.dxy.lc.model.bean.FileUploadDto;
import com.dxy.lc.model.entity.Attachment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.dxy.lc.base.Resp;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.dxy.lc.service.AttachmentService;

/**
 * <p>
 * 附件表 controller
 * </p>
 *
 * @author dxy
 * @since 2024-09-28
 */
@Slf4j
@RestController
@RequestMapping("/lc/attachment")
public class AttachmentController {

    @Autowired
    private AttachmentService service;


    @PostMapping("/delByIds")
    public Resp delByIds(@RequestBody List<Integer> ids) {
        service.delByIds(ids);
        return Resp.bizSuccess();
    }

    @GetMapping("/getDetail")
    public Resp<Attachment> getDetail(Integer id) {
        return Resp.bizSuccess(service.getDetail(id));
    }


    @PostMapping(value = {"/upload"}, consumes = {"multipart/form-data"})
    public Resp<Attachment> uploadFile(FileUploadDto dto) {
        List<Attachment> list = service.batchUpload(Collections.singletonList(dto));
        return Resp.bizSuccess(list.get(0));
    }

    @PostMapping(value = {"/batchUpload"}, consumes = {"multipart/form-data"})
    public Resp<List<Attachment>> batchUploadFile(List<FileUploadDto> dtoList) {
        return Resp.bizSuccess(service.batchUpload(dtoList));
    }

    // type 设置响应头类型  "inline"查看  "attachment"下载
    @GetMapping(value = {"/get/{type}/{id}"}, produces = {"application/octet-stream"})
    public ResponseEntity<Resource> getFile(@PathVariable("type") String type,
                                            @PathVariable("id") Long id) {
        return service.getFile(type, id);
    }

    @GetMapping("/getFileList")
    public Resp<List<Attachment>> getFileList(String bizType, String bizId) {
        return Resp.bizSuccess(service.getFileList(bizType, bizId));
    }

    @PostMapping("/delByBizTypeAndBizId")
    public Resp delByBizTypeAndBizId(@RequestBody Map<String, String> params) {
        String bizType = params.get("bizType");
        String bizId = params.get("bizId");
        service.delByBizTypeAndBizId(bizType, bizId);
        return Resp.bizSuccess();
    }
}